import app from '@/main'

/**
 * 构建表单数据
 * @param formItems
 * @param id
 * @returns {Promise<unknown>}
 */
export function mapForm(formItems, id) {
	

  return new Promise(function (resolve, reject) {
    let form = {};
    for (let i = 0; i < formItems.length; i++) {
      if (formItems[i].type === 'manyInput') {
        for (let k in formItems[i].key) {
          form[formItems[i].key[k]] = formItems[i].value[k]
        }
      } else if (formItems[i].type === 'actionSheet') {// 多级联动分类，value为对象
        form[formItems[i].key] = formItems[i].value.value
		if (formItems[i].child) {
			form[formItems[i].child.key] = formItems[i].child.value.value
		} 
      } else if (formItems[i].type === 'city') {// 匹配成uView需要格式
        if (formItems[i].value !== null) {

          if (formItems[i].value.city) {
            form[formItems[i].key] = [
              {label: formItems[i].value.country_name, value: formItems[i].value.country},
              {label: formItems[i].value.province_name, value: formItems[i].value.province},
              {label: formItems[i].value.city_name, value: formItems[i].value.city},
            ]
          } else {
            form[formItems[i].key] = formItems[i].value
          }
        }
      
      }else {
		  // if(formItems[i].type == 'radio'){
			 //  debugger
		  // }
        form[formItems[i].key] = formItems[i].value
      }
    }
	
	
    form.id = id
    if (!form) {
      reject('表单数据错误')
    }
    // console.log('form', form)
    resolve(form)
  })
}


/**
 * 表单项匹配
 * @param formItems
 * @param details
 * @returns {Promise<unknown>}
 */
export function mapFormItems(formItems, details) {
  return new Promise(function (resolve, reject) {  
    for (let i = 0; i < formItems.length; i++) {
      // 特殊字段处理
      if (formItems[i].type === 'actionSheet') {
        formItems[i].value = details[formItems[i].key] || []
        if (formItems[i].child){
			
			if((details[formItems[i].key].child!=undefined)&&(details[formItems[i].key].child!='undefined')) {
			  formItems[i].child.value = details[formItems[i].key].child
			}
		}
      } else if (formItems[i].type === 'manyInput') {
        
		if (details.car_l!=0&&details.goods_l==0){
			formItems[i].value = [details.car_l, details.car_w, details.car_h]
        }
		if (details.goods_l!=0&&details.car_l==0) {
		  formItems[i].value = [details.goods_l, details.goods_w, details.goods_h]
		}
		
      } else if (formItems[i].type === 'city') {
        // TODO::暂时组件内处理
        formItems[i].value = details[formItems[i].key] || []
      } else if (formItems[i].type === 'checkbox') {
        // TODO::暂时组件内处理
        formItems[i].value.value = details[formItems[i].key] || ''
      } else if (formItems[i].type === 'radio') {
        // TODO::暂时组件内处理	
		var radio_value =details[formItems[i].key];		
		if(details[app.$i18n.t('item.'+formItems[i].key)]){
			formItems[i].value = details[app.$i18n.t('item.'+formItems[i].key)]
		}else if(radio_value){
			formItems[i].value = radio_value
		}else{
			formItems[i].value = ''
		}	
		
	  } else if (formItems[i].type === 'text' || formItems[i].type === 'textarea') {
        const lang = uni.getStorageSync('lang')
        if (lang === 'ru') {
          formItems[i].value = details[app.$i18n.t('item.' + formItems[i].key)]
        } else {
          formItems[i].value = details[formItems[i].key]
        }
      } else {
        formItems[i].value = details[formItems[i].key]
      }
    }
    // console.log('formItems', formItems)
    if (!formItems) {
      reject('表单项匹配错误')
    }
    resolve(formItems)
  })
}

function isObject(value) {
  return typeof value === 'object' && value !== null;
}

/**
 * 表单验证
 * @param formItems
 * @param formData
 * @param vm
 */
export function formRules(formItems, formData, vm) {
  for (let i = 0; i < formItems.length; i++) {
	if (formItems[i].required) {
	  if(formData[formItems[i].key]!=0){ 
		  if (!formData[formItems[i].key] || formData[formItems[i].key] === '' || formData[formItems[i].key].length === 0) {
		    vm.$u.toast(app.$i18n.t(formItems[i].placeholder))
		    return false
		  }
		  
		  if(isObject(formData[formItems[i].key])){
			  if(formData[formItems[i].key].value==''){
				  vm.$u.toast(app.$i18n.t(formItems[i].placeholder))
				  return false
			  }
		  }
		  
	  }else{
		  if ((formData[formItems[i].key] === '')|| (formData[formItems[i].key].length === 0)){
				vm.$u.toast(app.$i18n.t(formItems[i].placeholder))
				return false
		  }
		  
		  if(isObject(formData[formItems[i].key])){
			  if(formData[formItems[i].key].value==''){
				  vm.$u.toast(app.$i18n.t(formItems[i].placeholder))
				  return false
			  }
		  }
	  }
	}
  }
  return true
}
